﻿using Ewell.BizComponent.Charge.Model;
using Ewell.BizComponent.Service.Model.Request;
using Ewell.BizComponent.Service.Server;
using FunnyArts.Wpf.ControlLibrary.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Ewell.BizComponent.Charge.UI.VM
{
    public class CashierMainVm: NotifyPropertyChanged
    {
        //左侧收费步骤
        private List<CashierStepVm> _cashierSteps;
        public List<CashierStepVm> cashierSteps { get { return _cashierSteps; } 
            set 
            { 
                if(_cashierSteps != value)
                {
                    _cashierSteps = value;
                    OnPropertyChanged(nameof(cashierSteps));
                }
            } }
        /// <summary>
        /// 当前步骤
        /// </summary>
        private CashierStepVm _currentStep;
        public CashierStepVm currentStep { get { return _currentStep; } set { 
                if(_currentStep != value)
                {
                    _currentStep = value;
                    OnPropertyChanged(nameof(currentStep));
                }
            } }
        /// <summary>
        /// 预算参数
        /// </summary>
        private OutPreSettlementRequest _outPreSettlementRequest;
        public OutPreSettlementRequest outPreSettlementRequest { get { return _outPreSettlementRequest; } set
            {
                if(_outPreSettlementRequest != value)
                {
                    _outPreSettlementRequest = value;
                    OnPropertyChanged(nameof(outPreSettlementRequest));
                }
            } }

        /// <summary>
        /// 当前窗体
        /// </summary>
        public Window localWindow { get; set; }
        /// <summary>
        /// 支付方式
        /// </summary>
        private List<PayWayVm> _payWayList;
        public List<PayWayVm> payWayList
        {
            get => _payWayList; set
            {
                if (value != _payWayList)
                {
                    _payWayList = value;
                    OnPropertyChanged(nameof(payWayList));
                }
            }
        }
        
        /// <summary>
        /// 患者信息
        /// </summary>
        private PatientVm _patientVm;
        public PatientVm patientVm
        {
            get => _patientVm; set
            {
                if (value != _patientVm)
                {
                    _patientVm = value;
                    OnPropertyChanged(nameof(patientVm));
                }
            }
        }
        /// <summary>
        /// 结算上下文
        /// </summary>
        public CashierContext context { get; set; }

        public CashierMainVm(PatientVm patientVm)
        {
            this.patientVm = patientVm;
        }
        public CashierMainVm(PatientVm patientVm, List<PayWayVm> payWayList, OutPreSettlementRequest outPreSettlementRequest) : this(patientVm)
        {
            this.payWayList = payWayList;
            this.outPreSettlementRequest = outPreSettlementRequest;
            //结算上下文
            context = new Model.CashierContext()
            {
                bizSourceCoding=outPreSettlementRequest.bizSourceCoding,
                menuId=outPreSettlementRequest.menuId,
                cashierMainVm = this,
                outPreSettlementRequest = outPreSettlementRequest,
                patientVm = patientVm
            };
            this.cashierSteps = new List<CashierStepVm>();
            //预算步骤
            var step = new CashierStepVm()
            {
                StepNumber = 1,
                StepName = "预结算",
                StepState = StepStatus.Wait,
                context = context,
                pageViewModel = new PreCashierVm(),
            };
            (step.pageViewModel as PreCashierVm).NoticeChargeCancel += this.OnPreCancel;
            (step.pageViewModel as PreCashierVm).NoticeChargeResult += this.OnPreCharged;
            step.stepLoading=step.pageViewModel.stepLoading;
            step.stepHandler=step.pageViewModel.stepHandler;
            this.cashierSteps.Add(step);

            //结算步骤
            step = new CashierStepVm()
            {
                StepNumber = 2,
                StepName = "结算",
                StepState = StepStatus.Wait,
                context = context,
                pageViewModel = new CashierNewVm(),
            };
            step.stepLoading = step.pageViewModel.stepLoading;
            step.stepHandler = step.pageViewModel.stepHandler;
            (step.pageViewModel as CashierNewVm).NoticeChargeCancel += this.OnChargeCancel;
            (step.pageViewModel as CashierNewVm).NoticeChargeResult += this.OnCharged;
            this.cashierSteps.Add(step);

            var enumerator = this.cashierSteps.GetEnumerator();
            this.currentStep = enumerator.MoveNext()? enumerator.Current:null;
        }

        public CashierStepVm next()
        {
            var currentStep = this.currentStep;
            CashierStepVm nextStep = default(CashierStepVm);

            var index =this.cashierSteps.IndexOf(currentStep);
            if(index< this.cashierSteps.Count - 1)
            {
                nextStep = this.cashierSteps[++index];
            }
            try
            {
                currentStep.stepHandler?.Invoke(currentStep.context);
                currentStep.StepState= StepStatus.Finash;
                if (nextStep!=null)
                {
                    nextStep.context = currentStep.context;
                    this.currentStep= nextStep;
                    nextStep.stepLoading.Invoke(nextStep.context);
                }
            }
            catch (Exception ex)
            {
                currentStep.errorMsg = ex.Message;
            }
            return nextStep;
        }

        /// <summary>
        /// 预算响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="result"></param>
        private void OnPreCharged(object sender, CashierContext context)
        {
            
        }
        /// <summary>
        /// 取消预算事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="context"></param>
        private void OnPreCancel(object sender, CashierContext context)
        {
            this.localWindow.DialogResult = false;
            this.localWindow.Close();
        }
        /// <summary>
        /// 结算响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="result"></param>
        private void OnCharged(object sender, CashierContext context)
        {
            MessageBox.Show("收费成功!");
            this.localWindow.DialogResult = true;
            this.localWindow.Close();
        }
        /// <summary>
        /// 结算取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="result"></param>
        private void OnChargeCancel(object sender, CashierContext context)
        {
            this.localWindow.DialogResult = false;
            this.localWindow.Close();
            //contentFrame.Visibility = Visibility.Collapsed;
            //OutRegisterServer outRegisterServer = new OutRegisterServer();
            //outRegisterServer.PreRegistCancel(new List<string>() { result.encounterId.ToString() });
        }
    }
}
